const App = getApp();
Page({
	data: {
		is_fw: false,
		is_text: '预约成功!',
		year: 0,
		month: 0,
		date: ['日', '一', '二', '三', '四', '五', '六'],
		dateArr: [],
		isToday: 0,
		isTodayWeek: false,
		todayIndex: 0,
		page_data: [],
		goods_id: 0,
		isToday8: 0,
		getDate: [],
		order_id: 0,
		sku_id: 0,
		datetime: 0,
		time_id: 0,
		address: {},
		type_id: 0,
		card_info: {},
	},
	SubmitFuwu: function() {
		wx.navigateBack();
	},
	DateDuan: function(e) {
		var time_id = e.currentTarget.id
		this.setData({
			time_id: time_id
		})
	},
	QueDingYy: function() {
		var goods_id = this.data.goods_id
		var order_id = this.data.order_id
		var sku_id = this.data.sku_id
		var address = this.data.address
		var appointment_date = this.data.isToday8
		var user_name = address.name
		var user_phone = address.phone
		var time_id = this.data.time_id
		var type_id = this.data.type_id
		let _this = this;
		App._post_form('appointment/page_submit', {
			type_id: type_id,
			appointment_date: appointment_date,
			user_name: user_name,
			user_phone: user_phone,
			address: address.address,
			time_id: time_id,
			order_id: order_id,
			goods_id: goods_id,
			sku_id: sku_id
		}, function(result) {
			if (result.msg = 'success') {
				_this.setData({
					is_fw: true,
				})
			}
		});
	},
	dateXuanze: function(e) {
		console.log(e)
		var isToday = e.currentTarget.dataset.date
		console.log(this.data.datetime)
		var today = this.data.datetime
		if (parseInt(isToday) < parseInt(today)) {
			wx.showModal({
				title: '温馨提示：',
				content: '不能选之前日期',
				showCancel: false,
				confirmText: '关闭提示',
				success(res) {

				}
			})
			return false
		}
		var isToday8 = e.currentTarget.dataset.datetime
		this.setData({
			isToday: isToday,
			isToday8: isToday8,
			time_id: 0,
		})
		this.getDate()
	},
	getDate: function() {
		var goods_id = this.data.goods_id
		var order_id = this.data.order_id
		var sku_id = this.data.sku_id
		var date = this.data.isToday8
		let _this = this;
		App._post_form2('appointment/page_data', {
			goods_id: goods_id,
			order_id: order_id,
			sku_id: sku_id,
			date: date
		}, function(result) {
			console.log(result)
			if (result.msg = 'success') {
				var getDate = result.data.list
				var time_id = 0
				if (getDate.length > 0) {
					time_id = getDate[0].time_id
				}
				var address = result.data.address
				var card_info = result.data.card_info
				_this.setData({
					getDate: getDate,
					time_id: time_id,
					address: address,
					card_info: card_info,
				})
			}
		});
	},
	onLoad: function(options) {
		var order_id = options.order_id
		var goods_id = options.goods_id
		var sku_id = options.sku_id
		var type_id = options.type_id
		this.setData({
			sku_id: sku_id,
			order_id: order_id,
			goods_id: goods_id,
			type_id: type_id
		})
		let now = new Date();
		let year = now.getFullYear();
		let month = now.getMonth() + 1;
		this.dateInit();
		this.setData({
			year: year,
			month: month,
			isToday: '' + year + month + now.getDate(),
			isToday8: '' + year + '-' + (month > 9 ? month : ('0' + month)) + '-' + (now.getDate() >
				9 ? now.getDate() : ('0' + now.getDate())),
			datetime: parseInt('' + year + month + now.getDate()),
		})

	},
	onShow: function() {
		this.getDate()
	},
	dateInit: function(setYear, setMonth) {
		//全部时间的月份都是按0~11基准，显示月份才+1
		let dateArr = []; //需要遍历的日历数组数据
		let arrLen = 0; //dateArr的数组长度
		let now = setYear ? new Date(setYear, setMonth) : new Date();
		let year = setYear || now.getFullYear();
		let nextYear = 0;
		let month = setMonth || now.getMonth(); //没有+1方便后面计算当月总天数
		let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
		let startWeek = new Date(year + '/' + (month + 1) + '/' + 1).getDay(); //目标月1号对应的星期
		let dayNums = new Date(year, nextMonth, 0).getDate(); //获取目标月有多少天
		let obj = {};
		let num = 0;

		if (month + 1 > 11) {
			nextYear = year + 1;
			dayNums = new Date(nextYear, nextMonth, 0).getDate();
		}
		arrLen = startWeek + dayNums;
		for (let i = 0; i < arrLen; i++) {
			if (i >= startWeek) {
				num = i - startWeek + 1;
				obj = {
					isToday: (parseInt('' + year + ((month + 1) > 9 ? (month + 1) : ('0' + (month + 1))) +(num<10?'0'+num:num))),
					dateNum: num,
					isToday8: '' + year + '-' + ((month + 1) > 9 ? (month + 1) : ('0' + (month + 1))) +
						'-' + (num > 9 ? num : ('0' + num)),
					weight: 5
				}
			} else {
				obj = {};
			}
			dateArr[i] = obj;
		}
		console.log(dateArr)
		this.setData({
			dateArr: dateArr
		})

		let nowDate = new Date();
		let nowYear = nowDate.getFullYear();
		let nowMonth = nowDate.getMonth() + 1;
		let nowWeek = nowDate.getDay();
		let getYear = setYear || nowYear;
		let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;

		if (nowYear == getYear && nowMonth == getMonth) {
			this.setData({
				isTodayWeek: true,
				todayIndex: nowWeek
			})
		} else {
			this.setData({
				isTodayWeek: false,
				todayIndex: -1
			})
		}
	},
	lastMonth: function() {
		//全部时间的月份都是按0~11基准，显示月份才+1
		let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
		let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
		this.setData({
			year: year,
			month: (month + 1)
		})
		this.dateInit(year, month);
	},
	nextMonth: function() {
		//全部时间的月份都是按0~11基准，显示月份才+1
		let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
		let month = this.data.month > 11 ? 0 : this.data.month;
		this.setData({
			year: year,
			month: (month + 1)
		})
		this.dateInit(year, month);
	}
})
